
//import com.adobe.ep.notifications.*;
import com.artesophy.library.NotiCallout;
import com.flexcapacitor.controls.WebView;
import com.freshplanet.nativeExtensions.*;

import flash.desktop.NativeApplication;
import flash.display.*;
import flash.events.Event;
import flash.events.GeolocationEvent;
import flash.events.KeyboardEvent;
import flash.events.LocationChangeEvent;
import flash.events.MouseEvent;
import flash.events.TouchEvent;
import flash.events.TransformGestureEvent;
import flash.filesystem.File;
import flash.geom.Rectangle;
import flash.media.StageWebView;
import flash.net.*;
import flash.net.URLRequest;
import flash.sampler.NewObjectSample;
import flash.sensors.Geolocation;
import flash.system.Capabilities;
import flash.text.TextField;
import flash.text.engine.SpaceJustifier;
import flash.ui.Keyboard;
import flash.utils.setTimeout;

import flashx.textLayout.elements.DivElement;
import flashx.textLayout.events.UpdateCompleteEvent;

import mx.collections.XMLListCollection;
import mx.events.FlexEvent;
import mx.events.ResizeEvent;
import mx.events.StateChangeEvent;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
import mx.utils.ObjectUtil;

import spark.events.ViewNavigatorEvent;
import spark.layouts.BasicLayout;

public var isStored:String = "FALSE";
public var userIdx:String = "";
public var lastNotiNum:int = 0;
public var hooked_idx:String = "";

public var app_log:String = "";
public var app_log_line_num:int = 1;

public var isNotiView:Boolean;

private const OUR_SITE_URL_START:String = "http://m.twitaddons.com";
private var TWEER_WRITE_URL:String = "http://m.twitaddons.com/page/tweet_detail.php";




/**
 * Utility method
 * Gets the correct path to the local directory. Example code posted by Mike Chambers
 * If the URL is incorrect (pointing to no page) then neither the location changing nor the 
 * change event are fired (in my tests).
 * */
private function getLocalPage(page:String):String {
	return new File(new File(page).nativePath).url;
}

private var appWidth:int;
private var appHeight:int;
protected var isChkedLogin:Boolean;
protected var isChkedLocalNotiInfo:Boolean;
protected var checkedShowGuide:Boolean;
private var initDone:Boolean = false;
private var closedInAppAd:Boolean;
protected function initProcess():void{
	
	initLoginDatabase();
	initNotificationDatabase();
	
	checkedShowGuide = false;
	
	isChkedLogin = false;
	checkLogin();
	
	isChkedLocalNotiInfo = false;
	checkLocalNotiInfo();
	
	try{
		NativeApplication.nativeApplication.addEventListener(KeyboardEvent.KEY_DOWN,backKeyDownEventHandler);
		NativeApplication.nativeApplication.addEventListener(KeyboardEvent.KEY_UP,backKeyUpEventHandler);
		
	}catch(e:Error){}
	
	//mainViewLoad();
	
	lab_setting_background.visible = false;			lab_setting_background.includeInLayout = false;
	vg_setting_back.visible = false;				vg_setting_back.includeInLayout = false;
	img_setting_panel.visible = false;				img_setting_panel.includeInLayout = false;
	
	//notiTest();
	//listenForPush();
	
	// show sppiner
	showSpinner();
	
	isNotiView = false;
	
	appWidth = this.width;
	appHeight = this.height;
	chkDeviceInfo();
	
	closedInAppAd = false;
	
	registerPushNotification();
	
	initDone = true;
}

private var pushNotification:PushNotification;
private function registerPushNotification():void{
	pushNotification = new PushNotification;
	if(pushNotification.isPushNotificationSupported){
		trace("isPushSupported = TRUE");
		pushNotification.addEventListener(PushNotificationEvent.PERMISSION_GIVEN_WITH_TOKEN_EVENT, receivedPushToken);
		
		//if(deviceInfo_os_name == "iOS"){
		//	trace("THIS IS iOS");
			pushNotification.registerForPushNotification();
		//}
	}
}

private function receivedPushToken(event:PushNotificationEvent):void{
	trace("received token : " + event.token);
	var deviceToken:String = event.token;
	httpserv_param = new Object();
	
	httpserv_param['idx'] = userIdx;
	httpserv_param['deviceToken'] = deviceToken;
	
	httpserv_saveDeviceToken.send(httpserv_param);
}

/** deviceIngo : s{ iOS | BB | Android } */
private var deviceInfo_os_name:String; // { iOS / Android {
private var deviceInfo_macaddress:String;
private var deviceInfo_gps_lng:String;
private var deviceInfo_gps_lat:String;
private var deviceInfo_gps_accuracy:String;
private function chkDeviceInfo():void{
	trace("checking device's informations");
	if(NetworkInfo.isSupported){
		var network:NetworkInfo = NetworkInfo.networkInfo;
		for each (var object:NetworkInterface in network.findInterfaces()){
			if(object.hardwareAddress){
				deviceInfo_macaddress = object.hardwareAddress;
			}
		}
	}else{
		deviceInfo_macaddress = "";
	}
	
	trace("checking geolocations...");
	try{
		var g:Geolocation;
		
		if (Geolocation.isSupported)
		{
			g = new Geolocation();
			
			if (g.muted)
			{
				deviceInfo_gps_lng = "";
				deviceInfo_gps_lat = "";
				deviceInfo_gps_accuracy = "";
			}
			
			g.addEventListener(GeolocationEvent.UPDATE, function(event:GeolocationEvent):void {
				deviceInfo_gps_lat = event.latitude.toString();
				deviceInfo_gps_lng = event.longitude.toString();
				deviceInfo_gps_accuracy = Math.sqrt( event.horizontalAccuracy^2 + event.verticalAccuracy^2 ).toString();
			}, false, 0, true);
		}
		else
		{
			deviceInfo_gps_lng = "";
			deviceInfo_gps_lat = "";
			deviceInfo_gps_accuracy = "";
		}
	}catch(e:Error){}
	
	
	trace("checking os's name");
	
	switch(true){
		case (Capabilities.version.indexOf('IOS') > -1):
			deviceInfo_os_name = "iOS"; 
			break;
		case (Capabilities.version.indexOf('QNX') > -1):
			deviceInfo_os_name = "BB"; // blackberry
			break;
		case (Capabilities.version.indexOf('AND') > -1):
			deviceInfo_os_name = "Android";
			break;
		default:
			deviceInfo_os_name = "Unknown";
			break;
	}
	
	
	
	trace("mac : " + deviceInfo_macaddress);
	trace("lng : " + deviceInfo_gps_lng);
	trace("lat : " + deviceInfo_gps_lat);
	trace("accuracy : " + deviceInfo_gps_accuracy);
}

protected function behaveProperLogin():void{
	//trace(isStored.toString());
	if(isStored == "TRUE"){
		//trace("ok");
		wv_main.source = "http://m.twitaddons.com/app_redirect.php?redirect_idx=" + userIdx;
		//trace("im going : " + "http://m.twitaddons.com/app_redirect.php?redirect_idx=" + userIdx);
	}else{
		//trace("no");
		mainViewLoad();
	}
	isChkedLogin = true;
}

protected function showSpinner():void{
	sp_webview.visible = true;
}

private var isMainViewLoading:Boolean = false;
protected function mainViewLoad():void{
	
	isMainViewLoading = true;
	wv_main.source = "http://m.twitaddons.com/index.php";
	
	//wv_main.loadURL("app:/callTest.html");
}

protected function checkLogin():void{
	// db 확인 후 isLogin, mId, mPermission 값 할당.
	getLogins();
	
}

protected function checkLocalNotiInfo():void{
	getNotifications();
}

private function stateChangedHandler(event:StateChangeEvent):void{
	switch (event.newState){
		case "basic":
			img_menuNaviBrowse.source = "assets/menu_icons_brows.png";
			img_menuNaviTwitter.source = "assets/menu_icons_twitter.png";
			img_menuNaviAddons.source = "assets/menu_icons_addons.png";
			break;
		case "popNaviExplorer":
			
			break;
		case "popNaviTwitter":
			break;
	}
	
	if(wv_main.source == TWEER_WRITE_URL && event.newState != "basic"){
		//img_selectPic.visible = false;
		//img_selectPic.includeInLayout = false;
	}
	
	if(wv_main.source == TWEER_WRITE_URL && event.newState == "basic"){
		img_selectPic.visible = true;
		img_selectPic.includeInLayout = true;
	}
}


private function onClickMenuHome():void{
	homeWebView();
}
private function onClickMenuBackward():void{
	if(wv_main.isHistoryBackEnabled){
		backWebView();
	}
}
private function onClickMenuForward():void{
	if(wv_main.isHistoryForwardEnabled){
		forwardWebView();	
	}
	img_menuForward.source = "assets/menu_icons_forward_glow.png";
}
private function onClickMenuRefresh():void{
	refreshWebView();	
	img_menuRefresh.source = "assets/menu_icons_refresh_glow.png";
}




protected function homeWebView():void{
	if(wv_main.source == "http://m.twitaddons.com/index.php" || wv_main.source == "http://m.twitaddons.com/"){
		goURL("http://m.twittadons.com/timeline.php");
		goURL("http://m.twitaddons.com/index.php");
	}else{
		goURL("http://m.twitaddons.com/index.php");
	}
	img_menuHome.source = "assets/menu_icons_home_glow.png";
	bi_top.visible = true;
}
protected function backWebView():void{
	wv_main.historyBack();
	try{
		img_menuBackward.source = "assets/menu_icons_back_glow.png";
	}catch(e:Error){}
	try{
		img_menuBackwardOnTop.source = "assets/menu_icons_back_glow.png";
	}catch(e:Error){}
	//img_selectPic.visible = false;
	//img_selectPic.includeInLayout = false;
	
	bi_top.visible = true;
}
protected function forwardWebView():void{
	if(wv_main.isHistoryForwardEnabled){
		wv_main.historyForward();
		img_menuForward.source = "assets/menu_icons_forward_glow.png";
		bi_top.visible = true;
	}
}
protected function refreshWebView():void{
	wv_main.reload();
	bi_top.visible = true;
}


private function onClickMenuTimeline():void{
	if(wv_main.source != "http://m.twitaddons.com/page/timeline.php"){
		goURL("http://m.twitaddons.com/page/timeline.php");
	}else{
		goURL("http://m.twitaddons.com/page/index.php"); // fake url
		goURL("http://m.twitaddons.com/page/timeline.php");
	}
	img_menuTimeline.source = "assets/menu_icons_timeline_glow.png";
	bi_top.visible = true;
}
private function onClickMenuMentions():void{
	if(wv_main.source != "http://m.twitaddons.com/page/timeline.php?mode=mentions"){
		goURL("http://m.twitaddons.com/page/timeline.php?mode=mentions");
	}else{
		goURL("http://m.twitaddons.com/page/timeline.php"); // fake url
		goURL("http://m.twitaddons.com/page/timeline.php?mode=mentions");
	}
	img_menuMentions.source = "assets/menu_icons_mentions_glow.png";
	bi_top.visible = true;
}
private function onClickMenuMessages():void{
	if(wv_main.source != "http://m.twitaddons.com/page/timeline.php?mode=direct_messages"){
		goURL("http://m.twitaddons.com/page/timeline.php?mode=direct_messages");
	}else{
		goURL("http://m.twitaddons.com/page/timeline.php"); //fake url
		goURL("http://m.twitaddons.com/page/timeline.php?mode=direct_messages");
	}
	img_menuMessages.source = "assets/menu_icons_dm_glow.png";
	bi_top.visible = true;
}
private function onClickMenuWrite():void{
	if(wv_main.source != "http://m.twitaddons.com/page/tweet_detail.php"){
		goURL("http://m.twitaddons.com/page/tweet_detail.php");
	}else{
		goURL("http://m.twitaddons.com/"); //fake url
		goURL("http://m.twitaddons.com/page/tweet_detail.php");
	}
	img_menuWrite.source = "assets/menu_icons_write_glow.png";
	bi_top.visible = true;
}

private function onClickMenuTwitResearch():void{
	if(wv_main.source != "http://m.twitaddons.com/page/sns_research.php"){
		goURL("http://m.twitaddons.com/page/sns_research.php");
	}else{
		goURL("http://m.twitaddons.com/"); // fake url
		goURL("http://m.twitaddons.com/page/sns_research.php");
	}
	img_menuTwitResearch.source = "assets/menu_icons_twitResearch_glow.png";
	bi_top.visible = true;
}
private function onClickMenuTwitAnnounce():void{
	if(wv_main.source != "http://m.twitaddons.com/page/marketing.php"){
		goURL("http://m.twitaddons.com/page/marketing.php");
	}else{
		goURL("http://m.twitaddons.com/"); // fake url
		goURL("http://m.twitaddons.com/page/marketing.php");
	}
	img_menuTwitAnnounce.source = "assets/menu_icons_twitAnnounce_glow.png";
	bi_top.visible = true;
}
private function onClickMenuFollowManager():void{
	if(wv_main.source != "http://m.twitaddons.com/page/fm.php"){
		goURL("http://m.twitaddons.com/page/fm.php");
	}else{
		goURL("http://m.twitaddons.com/"); // fake url
		goURL("http://m.twitaddons.com/page/fm.php");
	}
	img_menuFollowManager.source = "assets/menu_icons_followManager_glow.png";
	bi_top.visible = true;	
}
private function onClickMenuFollowCal():void{
	if(wv_main.source != "http://m.twitaddons.com/page/follow_rate.php"){
		goURL("http://m.twitaddons.com/page/follow_rate.php");
	}else{
		goURL("http://m.twitaddons.com/"); // fake url
		goURL("http://m.twitaddons.com/page/follow_rate.php");
	}
	img_menuFollowCal.source = "assets/menu_icons_followCal_glow.png";
	bi_top.visible = true;
}




private function onClickMenuNaviBrowse():void{
	if(this.currentState != "popNaviExplorer"){
		noti_callout.close();
		wv_main.snapshotMode = false;
		this.currentState = "popNaviExplorer";
		//wv_main.snapshotMode = true;
		img_menuNaviBrowse.source = "assets/menu_icons_brows_glow.png";
		img_menuNaviTwitter.source = "assets/menu_icons_twitter.png";
		img_menuNaviAddons.source = "assets/menu_icons_addons.png";
		
	}else{
		wv_main.snapshotMode = false;
		this.currentState = "basic";
		img_menuNaviBrowse.source = "assets/menu_icons_brows.png";
	}
}

private function onClickMenuNaviTwitter():void{
	if(this.currentState != "popNaviTwitter"){
		noti_callout.close();
		wv_main.snapshotMode = false;
		this.currentState = "popNaviTwitter";
		//wv_main.snapshotMode = true;
		img_menuNaviTwitter.source = "assets/menu_icons_twitter_glow.png";
		img_menuNaviBrowse.source = "assets/menu_icons_brows.png";
		img_menuNaviAddons.source = "assets/menu_icons_addons.png";
		
	}else{
		wv_main.snapshotMode = false;
		this.currentState = "basic";
		img_menuNaviTwitter.source = "assets/menu_icons_twitter.png";
	}
}

private function onClickMenuNaviAddons():void{
	if(this.currentState != "popNaviAddons"){
		noti_callout.close();
		wv_main.snapshotMode = false;
		this.currentState = "popNaviAddons";
		//wv_main.snapshotMode = true;
		img_menuNaviAddons.source = "assets/menu_icons_addons_glow.png";
		img_menuNaviTwitter.source = "assets/menu_icons_twitter.png";
		img_menuNaviBrowse.source = "assets/menu_icons_brows.png";
		
	}else{
		wv_main.snapshotMode = false;
		this.currentState = "basic";
		img_menuNaviAddons.source = "assets/menu_icons_addons.png";
	}
}


private var location_LOGED_IN:String = "http://m.twitaddons.com/?redirect_idx=";
private var location_writeTwit:String = "http://m.twitaddons.com/page/tweet_detail.php";
private var location_LOGOUT:String = "http://m.twitaddons.com/process/logout.php";

protected function changedURL(event:LocationChangeEvent):void{
	//trace("locationChange to : " + event.location);
	//trace("locationChange event : " + event);
	
	app_log += "locationChange to : " + event.location + "\r";
	
	if(event.location.substr(0, 38) == location_LOGED_IN){
		// idx 값만 떼기
		hooked_idx = event.location.substring(38);
		
		//trace("fullurl : " + event.location);
		//trace("hooked idx : " + hooked_idx);
		
		onSaveLogin();
		getLogins();
	}
	
	if(event.location.substr(0, 45) == location_writeTwit){
		// 트윗쓰기 페이지로 이동함
		// 메뉴바에 사진선택메뉴 추가 필요.
	}
	
	if(event.location.substr(0, 42) == location_LOGOUT){
		// logout
		//trace("chkdDD");
		onClickMenuDelLogin();
	}
	
	try{
		img_menuHome.source = "assets/menu_icons_home.png";
		//trace(img_menuHome.source.toString());
		/*
		img_menuBackward.source = "assets/menu_icons_back.png";
		img_menuForward.source = "assets/menu_icons_forward.png";
		img_menuRefresh.source = "assets/menu_icons_refresh.png";
		*/
	}catch(e:Error){
		
	}
	
	if(bi_top){
		bi_top.visible = false;
	}
	
	
	try{
		allSetNonGlow();
		bi_top.visible = false;
		
	}catch(e:Error){}
	
	
	//trace("chk Show");
	if(checkedShowGuide == false && isMainViewLoading == true && event.location == "http://m.twitaddons.com/"){
		//trace("isStore : " + isStored.toString());
		if(isStored == "FALSE"){
			//trace("show?");
			try{
				img_guidePanel.visible = true;
				wv_main.snapshotMode = true;
			}catch(e:Error){}
			//trace("showshow");
		}
		checkedShowGuide = true;
	}
	
	trace((wv_main.source.substr(0, TWEER_WRITE_URL.length) == TWEER_WRITE_URL).toString());
	if(wv_main.source.substr(0, TWEER_WRITE_URL.length) == TWEER_WRITE_URL){
		onClickAdClose();
		
		img_selectPic.visible = true;
		img_selectPic.includeInLayout = true;
	}else{
		if(img_inAppAd.visible == false){
			
		}else{
			
		}
		img_selectPic.visible = true;
		//img_selectPic.includeInLayout = false;
	}
	
	// 로그인되어 있고, 광고를 닫지 않았고, 광고를 불러오는 중이 아니고, 메인에 위치했을때만 광고 호출
	trace(isStored + " " +wv_main.source);
	if(!closedInAppAd && !callingAd && (isStored == "TRUE")){
		callingAd = true;
		callAdvertise();
		
		// get notification xml with calling advertises.
		httpserv_getNoti.send();
	}
	
	
}

public var xmlcol_noti:XMLListCollection = new XMLListCollection;
public function getNoti_result(event:ResultEvent):void{
	
	xmlcol_noti.removeAll();
	xmlcol_noti = new XMLListCollection(new XMLList(event.result.noti));
	
	//trace(xmlcol_noti.getItemAt(1).toString());
	
	compareNoti(lastNotiNum, int(xmlcol_noti[0].child("no")), int(xmlcol_noti[xmlcol_noti.length - 1].child("no")));
}

private function compareNoti(dbLastNo:int, xmlLastNo:int, xmlFirstNo:int):void{
	trace("comparing notification..");
	trace("params : " + dbLastNo + " " + xmlLastNo + " " + xmlFirstNo);
	var newNotiCount:int;
	if(dbLastNo <  xmlFirstNo){
		newNotiCount = xmlLastNo - xmlFirstNo + 1;
	}else if(xmlFirstNo <= dbLastNo && dbLastNo < xmlLastNo){
		newNotiCount = xmlLastNo - dbLastNo;
	}else if(xmlLastNo <= dbLastNo){
		newNotiCount = 0;
	}
	
	if(newNotiCount > 0){ callNotificaiton(newNotiCount); }
	
	trace("newNotiCount : " + newNotiCount);
}

public function onOpenedCallout():void{
	try{
		updateLocalNotiInfo();
	}catch(e:Error){}
}

private function updateLocalNotiInfo():void{
	lastNotiNum = xmlcol_noti[0].child("no");
	trace("received lasNo : " + lastNotiNum);
	onSaveNotification();
}


private var menuNotiBadgeCount:int = 0;
private function callNotificaiton(newNotiCount:int):void{
	// "n new notifications arrived".
	activateNotification(newNotiCount);
	menuNotiBadgeCount = newNotiCount;
	updateUnselectedMenuNotiBadge();
}

private function updateUnselectedMenuNotiBadge():void{
	try{
		if(menuNotiBadgeCount){
			var badgeCount:String = (menuNotiBadgeCount <= 4) ? menuNotiBadgeCount.toString() : "5";
			trace("badgeCoutn is : " + badgeCount);
			img_menuNoti.source = "assets/badge/menu_icons_noti_" + badgeCount + ".png";
		}else{
			img_menuNoti.source = "assets/badge/menu_icons_noti_0.png";
		}
	}catch(e:Error){
		img_menuNoti.source = "assets/badge/menu_icons_noti_0.png";
	}
}

private function httpserv_fail(event:FaultEvent):void{
	trace("httpserv failed : " + event.toString());
}

private function appearedBiTop():void{
	wv_main.snapshotMode = true;
	setTimeout(hideBiTop, 2500);
}

private function hideBiTop():void{
	bi_top.visible = false;
	
	allSetNonGlow();
}

private function hideSelectPic():void{
	img_selectPic.visible = false;
	img_selectPic.includeInLayout = false;
}

private function disappearedBiTop():void{
	
}

private function allSetNonGlow():void{
	this.currentState = "basic";
	try{
		img_menuBackwardOnTop.source = "assets/menu_icons_back.png";
		img_menuHome.source = "assets/menu_icons_home.png";
		img_menuBackward.source = "assets/menu_icons_back.png";
		img_menuForward.source = "assets/menu_icons_forward.png";
		img_menuRefresh.source = "assets/menu_icons_refresh.png";
	}catch(e:Error){}
	try{
		img_menuTimeline.source = "assets/menu_icons_timeline.png";
		img_menuMentions.source = "assets/menu_icons_mentions.png";
		img_menuMessages.source = "assets/menu_icons_messages.png";
		img_menuWrite.source = "assets/menu_icons_write.png";
	}catch(e:Error){}
	try{
		img_menuTwitResearch.source = "assets/menu_icons_twitResearch.png";
		img_menuTwitAnnounce.source = "assets/menu_icons_twitAnnounce.png";
		img_menuFollowManager.source = "assets/menu_icons_followManager.png";
		img_menuFollowCal.source = "assets/menu_icons_followCal.png";
	}catch(e:Error){}
	/*
	try{
		img_menuNaviBrowse.source = "assets/menu_icons_brows.png";
		img_menuNaviTwitter.source = "assets/menu_icons_twitter.png";
		img_menuNaviAddons.source = "assets/menu_icons_addons.png";
		
	}catch(e:Error){}
	*/
	wv_main.snapshotMode = false;
}


protected function touchOverHandler(event:TouchEvent):void
{
	// TODO Auto-generated method stub
	
}



protected function changingURL(event:LocationChangeEvent):void{
	trace ("locationChanging to : " + event.location);
	trace ("locationChanging event : " + event);
	
	if(event.location.toString().substr(0, OUR_SITE_URL_START.length) != OUR_SITE_URL_START &&
		event.location.toString().substr(0, "http://m.twitaddons.com/oauth".length) != "http://m.twitaddons.com/oauth" &&
		event.location.toString().substr(0, "https://twitter.com/oauth".length) != "https://twitter.com/oauth"
	){
		event.preventDefault(); 
		backWebView();
		navigateToURL( new URLRequest( event.location ) ); 
	}
	
	
	trace("changing....");
	wv_main.snapshotMode = true;
	
	
	
	//hg_selectPic.includeInLayout = false;
	//hg_selectPic.visible = false;
	trace(event.location.substr(0, TWEER_WRITE_URL.length));
	if(event.location.substr(0, TWEER_WRITE_URL.length) == TWEER_WRITE_URL){
		img_selectPic.includeInLayout = true;
		img_selectPic.visible = true;
	}else{
		//img_selectPic.includeInLayout = false;
		//img_selectPic.visible = false;
	}
}

private var callingAd:Boolean = false;
protected function webViewLoadComplete():void{
	// spinner 지우기
	sp_webview.visible = false;
	this.currentState = "basic";
	
	
}

private var httpserv_param:Object;
private function callAdvertise():void{
	httpserv_param = new Object();
	//httpserv_param['idx'] = userIdx;
	//httpserv_param['mac_address'] = deviceInfo_macaddress;
	//httpserv_param['gps_lng'] = deviceInfo_gps_lng;
	//httpserv_param['gps_lat'] = deviceInfo_gps_lat;
	//httpserv_param['gps_accuracy'] = deviceInfo_gps_accuracy;
	
	httpserv_param['client'] = "app.twitaddons";
	httpserv_param['item'] = "mobile";
	httpserv_param['json'] = "true";
	
	httpserv_getInAppAd.send(httpserv_param);
	trace("called ad");
}

private var xmlcol_adList:XMLListCollection = new XMLListCollection();
private var obj_adList:Object;
private var obj_adInfo:Object;
private function getInAppAd_result(event:ResultEvent):void{
	//xmlcol_adList.removeAll();
	//xmlcol_adList = new XMLListCollection(new XMLList(event.result.ad));
	
	try{
		obj_adList = JSON.parse(event.result.toString());
	
	//obj_adList = JSON.decode(event.result.toString());
	
		obj_adInfo = obj_adList.item[0];
	
	//trace(ObjectUtil.toString(obj_adList));
	//trace(obj_adList.toString());
	//trace("------------");
	//trace(obj_adList.client);
	//trace(obj_adList.item["adview_acount"].toString());
	//trace(obj_adList.item('adview_account'));
	//trace(arr_adInfo.toString());
	//trace(ObjectUtil.toString(obj_adInfo));
	//trace(obj_adInfo);
	//trace(ObjectUtil.toString(obj_adInfo));
	//trace("-------------------");
	//trace(obj_adInfo.adview_acount);
	
	//trace("i received : " + event.result.toString());
		trace("received ad info");
	
		showAdvertise();
	}catch(e:Error){}
}

private var inAppAd_width:Number;
private var inAppAd_height:Number;
private function showAdvertise():void{
	inAppAd_width = appWidth;
	inAppAd_height = appWidth * 120 / 640;
	
	//trace(xmlcol_adList[0].child('agent'));
	
	wv_main.height = appHeight * 0.82 - inAppAd_height;
	img_inAppAd.height = inAppAd_height;
	img_inAppAd.width = inAppAd_width;
	
	trace("setting image source");
	var source_url:String = obj_adInfo.item_image_url;
	
	img_inAppAd.source = source_url;
	img_inAppAd.visible = true;
	
	callingAd = false;
}

private function onClickInAppAd(event:MouseEvent):void{
	// x 값에 따라서 광고 클릭 / 혹은 / 광고 닫기 로 처리.
	//trace("90% of appWidth : " + 0.9 * event.stageX);
	//trace("appWidth : " + appWidth);
	if((event.stageX) <= (appWidth*0.9)){
		onClickAdContent();
	}else{
		onClickAdClose();
	}
}

private function onClickAdContent():void{
	var url:String = "";
	
	url += "http://gioo.net/adons/ad.php?";
	url += "c=" + obj_adInfo.t_campaign_idx;
	url += "&i=" + obj_adInfo.t_item_idx;
	url += "&t=" + obj_adList.client;
	url += "&u=" + obj_adInfo.item_url;
	url += "&e=" + obj_adList.item_type;
	
	trace("url is : " + url);
	
	navigateToURL(new URLRequest(url));
	onClickAdClose();
}

private function onClickAdClose():void{
	closeInAppAd();
	closedInAppAd = true;
}

private function closeInAppAd():void{
	img_inAppAd.visible = false;
	img_inAppAd.includeInLayout = false;
	wv_main.percentHeight = 82;
}
	
private function http_fault(event:FaultEvent):void{
	trace("event was fault : " + event.toString());
}

public function onCloseNoti():void{
	if(this.currentState == "basic" && lab_setting_background.visible != true){
		trace("33");
		wv_main.snapshotMode = false;
	}
	img_menuNoti.source = "assets/badge/menu_icons_noti_0.png";
}

public var noti_callout:NotiCallout = new NotiCallout();
public function onClickMenuNoti():void{
	//trace(wv_main.snapshotMode.toString());
	
	if(wv_main.snapshotMode == true && this.currentState == "basic"){
		wv_main.snapshotMode = false;
		noti_callout.close();
		img_menuNoti.source = "assets/badge/menu_icons_noti_0.png";
	}else{
		wv_main.snapshotMode = false;
		this.currentState = "basic";
		// out of callout can't be handling in set callout was modal:true 
		
		
		
		noti_callout.open(img_menuNoti, false);
		
		img_menuNoti.source = "assets/badge/menu_icons_noti_0_glow.png";
	}
	
	if(this.currentState != "basic"){
		this.currentState = "basic";
	}
}


protected function onClickMenuSetting():void{
	if(lab_setting_background.visible){
		onClickMenuSettingClose();
	}else{
		allSetNonGlow();
		showSettingMenu();
	}
}

protected function showSettingMenu():void{
	
	wv_main.snapshotMode = true;
	
	vg_setting_panel_back.visible = true; vg_setting_panel_back.includeInLayout = true;
	lab_setting_background.visible = true;	lab_setting_background.includeInLayout = true;
	vg_setting_back.visible = true;				vg_setting_back.includeInLayout = true;
	img_setting_panel.visible = true;			img_setting_panel.includeInLayout = true;
	
	if(isStored == "TRUE"){
		btn_setting_1.enabled = true;
		btn_setting_1.label = "로그아웃";
	}else{
		btn_setting_1.enabled = false;
		btn_setting_1.label = "로그인되지 않았습니다.";
	}
	
	trace("OS name is : "+deviceInfo_os_name);
	if(deviceInfo_os_name == "iOS"){
		btn_closeApp.includeInLayout = false;
		btn_closeApp.visible = false;
	}
	
	//tarea_log.text = app_log;
	
	this.currentState = "basic";
	img_menuNaviBrowse.source = "assets/menu_icons_brows.png";
	img_menuNaviTwitter.source = "assets/menu_icons_twitter.png";
	img_menuNaviAddons.source = "assets/menu_icons_addons.png";
	img_menuSetting.source = "assets/menu_icons_setting_glow.png";
	
	if(noti_callout.visible){
		noti_callout.close();
	}
}


protected function onClickMenuDelLogin():void{
	delLogin();
	onClickMenuSettingClose();
	logout();
}

private function logout():void{
	wv_main.source = "http://m.twitaddons.com/process/logout.php";
}

public function onClickMenuSettingClose():void{
	
	vg_setting_panel_back.visible = false;			vg_setting_panel_back.includeInLayout = false;
	lab_setting_background.visible = false;		lab_setting_background.includeInLayout = false;
	vg_setting_back.visible = false;				vg_setting_back.includeInLayout = false;
	img_setting_panel.visible = false;				img_setting_panel.includeInLayout = false;
	
	wv_main.snapshotMode = false;
	img_menuSetting.source = "assets/menu_icons_setting.png";
}

private function eff_resize_done_Handler():void{
	
}


private function closeNavigator():void{
	wv_main.snapshotMode = false;
	this.currentState = "basic";
	img_menuNaviBrowse.source = "assets/menu_icons_brows.png";
	img_menuNaviTwitter.source = "assets/menu_icons_twitter.png";
	img_menuNaviAddons.source = "assets/menu_icons_addons.png";
}

/*
private function onClickLogViewSwitch():void{
	if(tarea_log.visible == false){
		tarea_log.visible = true;
	}else{
		tarea_log.visible = false;
	}
}
*/

public function goURL(url:String):void{
	wv_main.source = url;
}

private var tempURL:String = "";
private function onClickTitle():void{
	
	// 우선은 그냥 리프레시로 사용.
	onClickMenuRefresh();
	
	/*
	tempURL = wv_main.source;
	wv_main.source = "javascript:$('html,body').animate({scrollTop: $('#timeline_top').offset().top}); ";
	trace(wv_main.source);
	*/
}

/*
protected function resizingApp(event:ResizeEvent):void
{
	// TODO Auto-generated method stub
	if(initDone){
		wv_main.snapshotMode = false;
		
		
	}
}
*/


/*
// orientation 전환 체크
private function resizedApp(event:FlexEvent):void
{
	// TODO Auto-generated method stub
	if(initDone){
		if(this.currentState != "basic"){
			wv_main.snapshotMode = true;
		}
		if(this.width > this.height){
			// landscape mode
			//trace("land");
			spac_middle.percentHeight = 77;
			img_bottomMenuBack.percentHeight = 15;
			hg_menu.percentHeight = 15;
			wv_main.percentHeight = 77;
			spac_settingPanelBottom.percentHeight = 15;
			spac_settingBottom.percentHeight = 15;
			img_setting_panel.percentHeight = 45;
			vg_setting.percentHeight = 45;
			//img_onNaviBack.percentHeight = 85;
		}else{
			// portrait mode
			//trace("port");
			spac_middle.percentHeight = 82;
			img_bottomMenuBack.percentHeight = 10;
			hg_menu.percentHeight = 10;
			wv_main.percentHeight = 82;
			spac_settingPanelBottom.percentHeight = 10;
			spac_settingBottom.percentHeight = 10;
			img_setting_panel.percentHeight = 30;
			vg_setting.percentHeight = 30;
			//img_onNaviBack.percentHeight = 90;
		}
	}
	
}
	*/

private function onClickGuidePanel():void{
	img_guidePanel.visible = false;
	wv_main.snapshotMode = false;
}

private function onClickMenuGuideLayer():void{
	onClickMenuSettingClose();
	wv_main.snapshotMode = true;
	img_guidePanel.visible = true;
}


private function onClickApplication(event:MouseEvent):void
{
	// TODO Auto-generated method stub
	trace("clicked target : " + event.target.toString());
}

private function onClickMenuSettingExit():void{
	wv_main.snapshotMode = true;
	Alert.show(this, "정말로 종료하시겠습니까?");
}


protected function gestureSwipeOnMenu(event:TransformGestureEvent):void
{
	// TODO Auto-generated method stub
	if(event.offsetX == 1){
		forwardWebView();
	}else if(event.offsetX == -1){
		backWebView();
	}
}


private function onClickMainTwitaddons():void{
	if(initDone == false){
		this.currentState = "loading";
		initProcess();
	}else{
		this.currentState = "basic";
	}
}
private function onClickMainGiftme():void{
	navigateToURL(new URLRequest("http://twitaddons.com/giftme/"));
}
private function onClickMainResearch():void{
	navigateToURL(new URLRequest("http://m.twitaddons.com/page/sns_research.php"));
}
private function onClickMainMovieons():void{
	navigateToURL(new URLRequest("http://movie.twitaddons.com/"));
}


private function onClickHelpCall():void{
	navigateToURL(new URLRequest("tel:1600-7178"));
}
private function onClickHelpMention():void{
	if(initDone == false){
		this.currentState = "loading";
		initProcess();
	}else{
		this.currentState = "basic";
	}
	goURL("http://m.twitaddons.com/page/tweet_detail.php?screen_name=giookr");
}


private function backClick():void{
	trace("visible chk : " + lab_setting_background.visible);
	
	if(img_guidePanel.visible){
		onClickGuidePanel();
	}else if(img_menuNoti.source == "assets/badge/menu_icons_noti_0_glow.png"){
		trace("close chk 0");
		onClickMenuNoti();
	}else if(lab_setting_background.visible && wv_main.snapshotMode){
		trace("close chk");
		onClickMenuSettingClose();
	}else if(!bi_top.visible && (this.currentState == "basic")){
		onClickMenuBackward();
	}else if(this.currentState != "basic"){
		this.currentState = "basic";
	}
}

public var alertShowYb:Boolean = false;
//--------------- 팝업 관리-------------------
protected function application_keyUpHandler(event:KeyboardEvent):void
{
	// TODO Auto-generated method stub
	switch(event.keyCode){
		case "Keyboard.BACK":
			//Alert.show(this, "정말로 종료하시겠습니까?");
			break;
	}
}
protected function backKeyUpEventHandler(event:KeyboardEvent):void
{
	if(event.keyCode == Keyboard.BACK){
		event.preventDefault();
		/*
		if(alertShowYb == false){
			//wv_main.snapshotMode = true;
			//Alert.show(this, "정말로 종료하시겠습니까?");
		}
		*/
		backClick();
		
		
	}
}

protected function backKeyDownEventHandler(event:KeyboardEvent):void
{
	if(event.keyCode == Keyboard.BACK){
		event.preventDefault();
	}
}
//--------------- 팝업 관리 -------------------